/**
*@Author zhengwei
**/
package org.lanqiao.webfm.servlet;

import java.util.*;
import java.io.Serializable;
import java.sql.*;
import java.lang.reflect.*;

import javacommon.base.*;
import javacommon.ijdbc.*;
import javacommon.util.*;
import javacommon.web.page.*;
import javacommon.web.util.ServletRequestUtils;

import org.lanqiao.webfm.model.*;
import org.lanqiao.webfm.model.Type;
import org.slf4j.*;
import org.lanqiao.webfm.service.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.commons.beanutils.BeanUtils;

/**
 * Type增删改查Servlet
 * 
 */
@WebServlet(urlPatterns = { "/Type/*" }) 
public class TypeServlet extends BaseServlet {
	private static final Logger LOGGER = LoggerFactory.getLogger(TypeServlet.class);
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	private TypeService typeService = TypeService.getInstance();

	// 新增之前
	protected void doCreateInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException , SQLException{
		request.getRequestDispatcher("/pages/Type/create.jsp").forward(
				request, response);
	}
	// 保存新增的对象
	protected void doSaveInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, SQLException {
		saveOrupdate(Type.class, request, response,"save");
	}
	// 查询出数据呈现在表单中供用户修改
	protected void doEditInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException , SQLException{
		String id = request.getParameter("id");
		Type type = typeService.getById(id);
		request.setAttribute("model", type);
		request.getRequestDispatcher("/pages/Type/edit.jsp").forward(request,
				response);
	}
	// 执行更改
	protected void doUpdateInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException , SQLException{
		saveOrupdate(Type.class, request, response,"update");
	}
	
	// 执行删除
	protected void doDeleteInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, SQLException {
		// 1.获取表单数据,也就是用户选择的所有的id
		String[] ids = request.getParameterValues("items");
		typeService.removeByIds(ids);
		response.sendRedirect(request.getContextPath()+"/Type/list.do");
	}
	
	//列表
	public void doListInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, SQLException {
		PageRequest<Type> pageRequest = new Type();
		populateQuery(pageRequest, DEFAULT_SORT_COLUMNS, request);
		Page page = typeService.findPage(pageRequest);
		savePage(page, pageRequest, request);
		request.getRequestDispatcher("/pages/Type/list.jsp").forward(request, response);
	}
	//展示
	protected void doShowInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, SQLException {
		//如果是联合主键，这些代码需要修改
		String id = request.getParameter("id");
		Type type = typeService.getById(id);
		request.setAttribute("model", type);
		request.getRequestDispatcher("/pages/Type/show.jsp").forward(request,
				response);
	}	
	
	protected void saveOrupdate(Class<Type> clazz, HttpServletRequest request, HttpServletResponse response,String op)
			throws ServletException, IOException , SQLException{
		Type type = new Type();
		//从request中拷贝属性，要求请求参数和Type的属性一致
		Map<String, Object> params = ServletRequestUtils.getParametersStartingWith(request, "");
		try {
			BeanUtils.copyProperties(type, params);//执行拷贝
			if(op.equals("save")){
				typeService.save(type);
				response.sendRedirect(request.getContextPath()+"/Type/list.do");
			}
			if(op.equals("update")){
				typeService.update(type);
				response.sendRedirect(request.getContextPath()+"/Type/show.do?id="+request.getParameter("id"));
			}
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException("beanUtils.copyProperties() error",e);
		} catch (InvocationTargetException e) {
			throw new IllegalArgumentException("beanUtils.copyProperties() error",e.getTargetException());
		}
	}
}
